package com.mapapp.flutter_mapapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.adapter.NewTaskAdapter;
import com.mapapp.flutter_mapapp.bean.DetailTree;
import com.mapapp.flutter_mapapp.bean.HomePageShouldBeRefreshed;
import com.mapapp.flutter_mapapp.bean.TaskItem;
import com.mapapp.flutter_mapapp.persenter.NewTaskPresenter;
import com.mapapp.flutter_mapapp.util.RxBus;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.NewTaskViewInterface;
import com.mapapp.flutter_mapapp.widget.NewTaskConfirmDialog;
import com.mapapp.flutter_mapapp.widget.TitleBar;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewTaskActivity extends ImmersiveBaseActivity implements NewTaskViewInterface {

    @BindView(R.id.rv_new_task)
    RecyclerView rvNewTask;
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    private NewTaskAdapter newTaskAdapter;

    private NewTaskPresenter presenter;

    private int state;

    private int position;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_task);
        ButterKnife.bind(this);
        presenter = new NewTaskPresenter(this, this, lifecycleSubject);


        String json = getIntent().getStringExtra("taskItems");
        final boolean isNew = getIntent().getBooleanExtra("isNew", true);
        Type listType = GsonUtils.getListType(TaskItem.class);
        List<TaskItem> list = GsonUtils.fromJson(json, listType);


        newTaskAdapter = new NewTaskAdapter(R.layout.item_new_task, list);
        rvNewTask.setLayoutManager(new LinearLayoutManager(mContext));
        rvNewTask.setAdapter(newTaskAdapter);

        newTaskAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                final TaskItem taskItem = newTaskAdapter.getData().get(position);
                String taskId = taskItem.getId();
                if (isNew) {
                    NewTaskConfirmDialog newTaskConfirmDialog = new NewTaskConfirmDialog(mContext);
                    newTaskConfirmDialog.showPopupWindow();
                    newTaskConfirmDialog.setListener(new NewTaskConfirmDialog.OnEnsureClickListener() {
                        @Override
                        public void onClick(int state) {
                            NewTaskActivity.this.state = state;
                            NewTaskActivity.this.position = position;
                            presenter.acceptOrRefuseTask(taskItem.getId(), state);
                        }
                    });
                } else {
                    Intent intent = new Intent(mContext, RegionSelectionActivity.class);
                    intent.putExtra("task_id", taskId);
                    startActivity(intent);
                }

            }
        });

        titleBar.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void showResult(List<DetailTree> detailTreeList) {
        if (state == 1) {
            //
        } else {
            ToastUtils.showMessage(mContext, "已拒绝");
        }
        RxBus.get().post("refresh", new HomePageShouldBeRefreshed());
        newTaskAdapter.remove(position);
    }
}
